# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=antlr4
pkgbase=mingw-w64-${_realname}-runtime-cpp
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-runtime-cpp"
pkgver=4.13.2
pkgrel=1
pkgdesc="ANother Tool for Language Recognition v4 (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.antlr.org/"
msys2_repository_url="https://github.com/antlr/antlr4"
msys2_references=(
  'archlinux: antlr4-runtime'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/antlr/antlr4/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0002-cmake-remove-error-msg.patch")
sha256sums=('9f18272a9b32b622835a3365f850dd1063d60f5045fb1e12ce475ae6e18a35bb'
            '5122ce9c37f2054792d3136c02b165f413a48434fecbcc2bdb6d37fd202bb139')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0002-cmake-remove-error-msg.patch"
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DCMAKE_CXX_STANDARD=17 \
    -DANTLR4_INSTALL=ON \
    -DANTLR_BUILD_CPP_TESTS=OFF \
    "../${_realname}-${pkgver}/runtime/Cpp"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .
}
